package com.dm.library.widgets;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.content.res.ResourcesCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dm.library.R;
import com.dm.library.utils.DensityUtil;
import com.dm.library.widgets.element.DTextView;


/**
 * 版    权:  深圳市迪蒙网络科技有限公司
 * 描    述:  <描述>
 * Created by sushuo on 2017/02/23.
 */

public class TwoLineLinearLayout extends LinearLayout {
    private static final int DEFAULT_TEXT_COLOR = Color.parseColor("#ff666666");
    private static final int DEFAULT_TEXT_SIZE = 14;// sp
    private static final int RIGHT_DRAWABLE_PADDING = 8;// dp
    private static final boolean IS_SHOW_BOTTOM_LINE = true;

    private DTextView rightTextView;
    private DTextView leftTextViewTop;
    private DTextView leftTextViewBottom;
    private View vBottomLine;
    private boolean isShowBottomLine;

    public TwoLineLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public TwoLineLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    public TwoLineLinearLayout(Context context) {
        super(context);
        initView(context, null);
    }

    private void initView(Context context, AttributeSet attrs) {
        // attachToRoot传入true代表layout文件填充的View会被直接添加进ViewGroup，而传入false则代表创建的View会以其他方式被添加进ViewGroup。
        View view = LayoutInflater.from(context).inflate(R.layout.two_line_layout, this);// 相当于attachToRoot为true
        rightTextView = (DTextView) view.findViewById(R.id.text_img_right);
        leftTextViewTop = (DTextView) view.findViewById(R.id.text_img_left_top);
        leftTextViewBottom = (DTextView) view.findViewById(R.id.text_img_left_bottom);
        vBottomLine = view.findViewById(R.id.v_bottom_line);

        if (null != attrs && !isInEditMode()) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TwoLineLinearLayout);
            isShowBottomLine = typedArray.getBoolean(R.styleable.TwoLineLinearLayout_isShowBottomLineTwo, IS_SHOW_BOTTOM_LINE);
            int rightTextColor = typedArray.getColor(R.styleable.TwoLineLinearLayout_rightTextColorTwo, DEFAULT_TEXT_COLOR);
            int leftTextColorTop = typedArray.getColor(R.styleable.TwoLineLinearLayout_leftTextColorTopTwo, DEFAULT_TEXT_COLOR);
            int leftTextColorBottom = typedArray.getColor(R.styleable.TwoLineLinearLayout_leftTextColorBottomTwo, DEFAULT_TEXT_COLOR);

            int rightTextSize = typedArray.getDimensionPixelSize(R.styleable.TwoLineLinearLayout_rightTextSizeTwo, DensityUtil.sp2px(context, DEFAULT_TEXT_SIZE));
            int leftTextSizeTop = typedArray.getDimensionPixelSize(R.styleable.TwoLineLinearLayout_leftTextSizeTopTwo, DensityUtil.sp2px(context, DEFAULT_TEXT_SIZE));
            int leftTextSizeBottom = typedArray.getDimensionPixelSize(R.styleable.TwoLineLinearLayout_leftTextSizeBottomTwo, DensityUtil.sp2px(context, DEFAULT_TEXT_SIZE));


            String rightTextContent = typedArray.getString(R.styleable.TwoLineLinearLayout_rightTextContentTwo);
            String leftTextContentTop = typedArray.getString(R.styleable.TwoLineLinearLayout_leftTextContentTopTwo);
            String leftTextContentBottom = typedArray.getString(R.styleable.TwoLineLinearLayout_leftTextContentBottomTwo);

            int rightDrawableResourceId = typedArray.getResourceId(R.styleable.TwoLineLinearLayout_rightDrawableResourceIdTwo, 0);

            leftTextViewTop.setTextColor(leftTextColorTop);
            leftTextViewTop.setTextSize(TypedValue.COMPLEX_UNIT_PX, leftTextSizeTop);
            leftTextViewTop.setTextContent(leftTextContentTop);

            leftTextViewBottom.setTextColor(leftTextColorBottom);
            leftTextViewBottom.setTextSize(TypedValue.COMPLEX_UNIT_PX, leftTextSizeBottom);
            leftTextViewBottom.setTextContent(leftTextContentBottom);

            rightTextView.setTextColor(rightTextColor);
            rightTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, rightTextSize);
            setTextViewContent(rightTextView, null != rightTextContent ? rightTextContent : "", rightDrawableResourceId, false);

            int rightDrawablePadding = typedArray.getDimensionPixelSize(R.styleable.TwoLineLinearLayout_rightDrawablePaddingTwo, DensityUtil.dip2px(context, RIGHT_DRAWABLE_PADDING));
            rightTextView.setCompoundDrawablePadding(rightDrawablePadding);

            vBottomLine.setVisibility(isShowBottomLine ? VISIBLE : GONE);
            typedArray.recycle();
        }
    }


    public void isShowBottomLine(boolean showBottomLine) {
        this.isShowBottomLine = showBottomLine;
        vBottomLine.setVisibility(isShowBottomLine ? VISIBLE : GONE);
    }

    /***
     * 设置左边文字
     *
     * @param text
     */
    public void setLeftContent(String text) {
        if (null != leftTextViewTop) {
            setTextViewContent(leftTextViewTop, text, 0, true);
        }
    }


    /***
     * 设置右边文字
     */
    public void setRightContent(String text) {
        if (null != rightTextView) {
            rightTextView.setTextContent(text);
        }
    }

    /***
     * 设置右边图片
     */
    public void setRightImg(int resourceId) {
        if (null != rightTextView) {
            setTextViewContent(rightTextView, null, resourceId, false);
        }
    }

    /***
     * 设置右边文字和图片
     */
    public void setRightContent(String text, int resourceId) {
        if (null != rightTextView) {
            setTextViewContent(rightTextView, text, resourceId, false);
        }
    }

    /**
     * 清除右边图标
     */
    public void clearRightImg() {
        if (null != rightTextView) {
            rightTextView.setCompoundDrawables(null, null, null, null);
        }
    }

    /***
     * @param textView    控件
     * @param content     内容
     * @param resourceId  资源ID
     * @param leftOrRight 图片在左边还是右边 true 左边
     */
    private void setTextViewContent(TextView textView, String content, int resourceId, boolean leftOrRight) {
        if (!TextUtils.isEmpty(content)) {
            textView.setText(content);
        }
        if (resourceId <= 0) {
            return;
        }
        try {
            Drawable drawable = ResourcesCompat.getDrawable(getResources(), resourceId, null);
            if (null != drawable) {
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//必须设置图片大小，否则不显示
                if (leftOrRight) {
                    textView.setCompoundDrawables(drawable, null, null, null);
                } else {
                    textView.setCompoundDrawables(null, null, drawable, null);
                }
            } else {
                textView.setCompoundDrawables(null, null, null, null);
            }
        } catch (Resources.NotFoundException e) {
            textView.setCompoundDrawables(null, null, null, null);
        }

    }


    public void setLeftTextViewTop(String content) {
        if (null != leftTextViewTop) {
            leftTextViewTop.setTextContent(content);
        }
    }

    public void setLeftTextBottom(String content) {
        if (null != leftTextViewBottom) {
            leftTextViewBottom.setTextContent(content);
        }
    }


    public void setRightTextColor(int color) {
        if (null != rightTextView) {
            rightTextView.setTextColor(getResources().getColor(color));
        }
    }

    public void setLeftTopTextColor(int color) {
        if (null != leftTextViewTop) {
            leftTextViewTop.setTextColor(getResources().getColor(color));
        }
    }


    public void setLeftBottomTextColor(int color) {
        if (null != leftTextViewBottom) {
            leftTextViewBottom.setTextColor(getResources().getColor(color));
        }
    }


    public String getRightText() {
        return null == rightTextView ? "" : rightTextView.getText().toString().trim();
    }

    public String getLeftTopText() {
        return null == leftTextViewTop ? "" : leftTextViewTop.getText().toString().trim();
    }

    public String getLeftBottomText() {
        return null == leftTextViewBottom ? "" : leftTextViewTop.getText().toString().trim();
    }

}
